const superagent=require('superagent');
const utilMath=require('./util');
const model=require('./model');
const http=require('https')
const cheerio=require('cheerio');
const fs=require('fs');
const reptileUrl="https://movie.douban.com/top250";
const Work=model.getModel('work');
const WorkShort=model.getModel('work_short');
const WorkLong=model.getModel('work_long');
let page=0;
let urlArray=[];
let longUrlArray=[];
let imgUrlArray=[];
let publicPath='./public/img'

getTotalInfo(0)


function getTotalInfo(page){
  console.log("getTotalInfo", page);
  http.get(`${reptileUrl}?start=${page}`,function (req, res) {
    let html='';
    let pageUrl='';

    req.on('data',function(data){
      html+=data;
    })
    req.on('end',function () {
      let $=cheerio.load(html);


      $(".grid_view li .item .hd a").each(function (i, elem) {
        pageUrl=$(elem).attr('href');
        urlArray.push(pageUrl)
      })
      if(page<225){
        setTimeout(function() {
          page+=25;
          getTotalInfo(page)
        },utilMath.randomNum(5000,8000))
      }else{
        console.log(urlArray.length);
        getPageInfo(0)
      }
    })
  })
}

function getPageInfo(sn) {
  console.log("getPageInfo", sn);
//urlArray[sn]
  http.get(
    // urlArray[sn]
    "https://movie.douban.com/subject/1292052/"
    ,function (req, res) {
    let html='';
    req.on('data',function(data){
      html+=data;
    })
    req.on('end',function () {
      let $=cheerio.load(html);
      // saveMovieLongs(html,'123');
      // return;
      const _title=$("h1 span").eq(0).text();
      const _imgUrl=$(".nbgnbg img").attr("src");
      const _picName=getPicName(_imgUrl);
      const shortInfo=$('.subject #info').html();
      const shortInfoArray=shortInfo.split('<br>');
      const _director=$('.subject #info .pl').eq(0).next().text();
      const _writer=$('.subject #info .pl').eq(1).next().text();
      const _actor=$('.subject #info .pl').eq(2).next().text();
      const _mType=$('.subject #info .pl').eq(3).next().text();
      const _country=getShortInfoArray(html,shortInfoArray,4);
      const _language=getShortInfoArray(html,shortInfoArray,5);
      const _show_date=getShortInfoArray(html,shortInfoArray,6);
      const _other=getShortInfoArray(html,shortInfoArray,8);
      const _imDb=$('.subject #info .pl').eq(9).next().attr('href');
      const _info=$('.indent').find('.all').text();


      saveMovieImage(_imgUrl,'movies')

      const workModel=new Work({
        'title':_title,
        'pic':_picName,
        'director':_director,
        'writer':_writer,
        'actor':_actor,
        'mType':_mType,
        'country':_country,
        'language':_language,
        'show_date':_show_date,
        'other':_other,
        'imDb':_imDb,
        'info':_info
      })
      workModel.save(function(e,d) {
        if(e){
          console.log('后端出错了');
        }
        const {_id}=d;
        console.log(_id);

        saveMovieShorts(html,_id);
        saveMovieLongs(html,_id);
      })
      if(sn<249){
        sn++;
        setTimeout(function(){
          getPageInfo(sn);
        },utilMath.randomNum(20000,40000))
      }else{
        console.log(longUrlArray);
        saveMovieLongsSingle(0)
      }
    })
  })
}

function getShortInfoArray(html,shortInfoArray,sn){
  let $=cheerio.load(html);
  return $(shortInfoArray[sn]).text().trim().split(":")[1]
}

function getPicName(url) {
  console.log(url);
  let imgNameArray=url.split('/');
  let imgName=imgNameArray[imgNameArray.length-1];
  return imgName;
}

function saveMovieShorts(html,work_id){
  let $=cheerio.load(html);
  let author='';
  let info='';
  let workShort=null;

  $(".comment").each(function (i, elem) {
    author=$(elem).find('h3 span').next().find('a').text()
    info=$(elem).find('p span').text()
    workShort=new WorkShort({work_id,author,info})
    workShort.save()
  })
}
function saveMovieLongs(html, work_id) {
  let $=cheerio.load(html);
  let _longUrl=null;
  let longUrlObj={};

  $(".main-bd").each(function (i, elem) {
    _longUrl=$(elem).find('h2').find('a').attr('href');
    longUrlObj["mUrl"]=_longUrl;
    longUrlObj["id"]=work_id
    longUrlArray.push(longUrlObj);
  })
  // saveMovieLongsSingle(0,work_id)
}

function saveMovieLongsSingle(sn) {
  console.log("saveMovieLongsSingle", sn);
  if(sn===longUrlArray.length){

    saveMovieImageSingle(0)
    return;
  }
  let url=longUrlArray[sn]["mUrl"];
  let _id=longUrlArray[sn]["id"];

  http.get(url,function (req, res) {
    let html='';
    req.on('data',function(data){
      html+=data;
    });
    let work_id=null;
    let imgUrl=null;
    let pic=null;
    let author=null;
    let title=null;
    let info=null;

    req.on('end',function () {
      let $=cheerio.load(html);
      work_id=_id;
      imgUrl=$(".main .avatar").find("img").attr("src");

      //被屏蔽间隔等待
      if(imgUrl===null || imgUrl.trim()===''){
        setTimeout(function(){
          saveMovieLongsSingle(sn,_id)
        },1000*60*60*2)
        return;
      }
      saveMovieImage(imgUrl,'avatar')
      pic=getPicName(imgUrl);

      author=$(".main-hd a").eq(0).text().trim();
      title=$(".article h1").text().trim();
      info=$("#link-report").find('p').text().trim();
      let worklongModel=new WorkLong({work_id,pic
        ,author,title,info})
      worklongModel.save(function (e,d) {
        if(e){
          console.log(e);
          return
        }
        if (sn%10===0){
          setTimeout(function(){
            saveMovieLongsSingle(sn+1,_id)
          },100000)
        } else{
          setTimeout(function(){
            saveMovieLongsSingle(sn+1,_id)
          },utilMath.randomNum(20000,30000))
        }


      })
    })
  })
}
function saveMovieImageSingle(sn) {
  console.log('saveMovieImageSingle', sn);
  if(sn===imgUrlArray.length){
    process.exit(0);
    return
  }
  let url=imgUrlArray[sn]['url'];
  let type=imgUrlArray[sn]['type']

  let imgName=getPicName(url);
  let path=publicPath+'/'+type+'/'+imgName;
  http.get(url,function (req, res) {
    var imageData='';

    req.setEncoding('binary');
    req.on('data',function (chunk) {
      imageData+=chunk
    })

    req.on('end',function () {

      fs.writeFile(path,imageData,'binary',function (err) {
        console.log('保存图片成功',path);
        saveMovieImageSingle(sn+1);
        // setTimeout(function () {
        //   saveMovieImageSingle(sn+1)
        // },utilMath.randomNum(8000,10000))
      })
    })
  })
}
function saveMovieImage(url,type){
  let imgObj={url,type}
  imgUrlArray.push(imgObj)
}
